import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class LotteryJFrame extends JFrame {
    private ImageIcon[] images = new ImageIcon[Main.list.size()];
    private int index = 0;
    JLabel imagejLabel = new JLabel();
    JLabel price = new JLabel();
    Thread imageThread;
    int count = 0;
    Music m = new Music();

    public LotteryJFrame() {
        initJFrame();
        initView();

        m.start();
        this.setVisible(true);
    }

    public void initJFrame() {
        this.setSize(700, 500);//设置宽高
        this.setTitle("抽奖系统");//设置标题
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭模式
        this.setLocationRelativeTo(null);//居中
        this.setAlwaysOnTop(true);//置顶
        this.setLayout(null);//取消内部默认布局
    }

    public void initView() {
        //添加图片
        for (int i = 0; i < Main.list.size(); i++) {
            images[i] = new ImageIcon(Main.list.get(i).getAddress());
        }
        imagejLabel.setIcon(images[0]);
        imagejLabel.setBounds(30, 20, 600, 337);
        //价格
        String s = Main.list.get(0).getName() + ":" + Main.list.get(0).getPrice();
        price.setText(s);
        price.setBounds(300, 350, 300, 50);
        price.setFont(new Font("华文中宋", 1, 20));
        this.add(price);
        startDelay(1000); // 初始轮换间隔为两秒

        this.getContentPane().add(imagejLabel);

        //抽奖按钮
        JButton lottery = new JButton("抽奖");
        lottery.setBounds(300, 400, 100, 50);
        lottery.setContentAreaFilled(false);
        this.getContentPane().add(lottery);
        lottery.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                count = 0;
                // 加快轮换速度
                startDelay(100);
                // 在2-5秒后停止轮换
                stopDelay();
            }
        });

        // 音乐按钮
        JButton jy = new JButton(new ImageIcon("picture/静音.png"));
        jy.setBounds(600, 400, 48, 50);
        jy.setContentAreaFilled(false);
        JButton bf = new JButton(new ImageIcon("picture/播放.png"));
        bf.setBounds(600, 400, 48, 50);

        this.getContentPane().add(bf);
        this.getContentPane().add(jy);
        jy.setVisible(false);
        bf.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bf.setVisible(false);
                jy.setVisible(true);
                m.pauseSound();
            }
        });
        jy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bf.setVisible(true);
                jy.setVisible(false);
                m.resumeSound();
            }
        });

        JLabel imagejLabel = new JLabel(new ImageIcon("picture/tc.jpg"));
        imagejLabel.setBounds(0, 0, 700, 500);
        this.getContentPane().add(imagejLabel);
    }

    public void startDelay(int time) {
        if (imageThread != null && imageThread.isAlive()) {
            imageThread.interrupt(); // 中断当前线程
        }
        imageThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!Thread.interrupted()) {
                        Random r = new Random();
                        index = r.nextInt(Main.list.size());
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                imagejLabel.setIcon(images[index]);
                                String s = Main.list.get(index).getName() + ":" + Main.list.get(index).getPrice();
                                price.setText(s);
                            }
                        });
                        Thread.sleep(time);
                    }
                } catch (InterruptedException e) {
                    // 线程被中断
                }
            }
        });
        imageThread.start();
    }

    public void stopDelay() {
        Random r = new Random();
        int n = r.nextInt(3000) + 2000; // 2-5秒的随机时间
        Timer timer = new Timer(n, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (imageThread != null && imageThread.isAlive()) {
                    imageThread.interrupt(); // 中断图片轮换线程
                    initJDialog();
                }
            }
        });
        timer.setRepeats(false); // 只执行一次
        timer.start();
    }

    public void initJDialog() {
        if (count == 0) {
            //创建一个弹框对象
            JDialog jDialog = new JDialog();
            //给弹框设置大小
            jDialog.setSize(300, 200);
            //让弹框置顶
            jDialog.setAlwaysOnTop(true);
            //让弹框居中
            jDialog.setLocationRelativeTo(null);
            //取消默认布局
            jDialog.setLayout(null);
            JLabel jLabel = new JLabel("恭喜获得" + Main.list.get(index).getName() + "，请添加收货信息");
            jLabel.setBounds(27, 30, 300, 30);
            jLabel.setFont(new Font("华文中宋", 1, 14));
            jDialog.getContentPane().add(jLabel);
            JButton addReceiv = new JButton("去添加");
            addReceiv.setBounds(100, 100, 100, 30);
            jDialog.getContentPane().add(addReceiv);
            jDialog.setVisible(true);
            addReceiv.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    new addJFrame();
                    jDialog.setVisible(false);
                }
            });

        }
        count++;
    }

}

